﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PTCapture.Support
{
    public static class MathFunctions
    {
        /// <summary>
        /// Get the smallest angle using a simplified scalar product.
        /// </summary>
        /// <param name="point">End point</param>
        /// <param name="point_2">The mid point</param>
        /// <param name="point_3">End point</param>
        /// <returns></returns>
        public static double GetAngle(Point endPointA, Point midPointB, Point endPointC)
        {
            double a_x = (double)endPointA.X;
            double a_y = (double)endPointA.Y;
            double b_x = (double)midPointB.X;       // Angle at this point
            double b_y = (double)midPointB.Y;       // Angle at this point
            double c_x = (double)endPointC.X;
            double c_y = (double)endPointC.Y;

            // Scalar product without sqrt
            double dotProd = (c_x - b_x) * (a_x - b_x) + (c_y - b_y) * (a_y - b_y);
            double len1squared = (a_x - b_x) * (a_x - b_x) + (a_y - b_y) * (a_y - b_y);
            double len2squared = (c_x - b_x) * (c_x - b_x) + (c_y - b_y) * (c_y - b_y);
            double angle = Math.Acos(dotProd / Math.Sqrt(len1squared * len2squared));

            angle *= 180 / Math.PI;

            return angle;
        }

        /// <summary>
        /// Area of a triangle.
        /// </summary>
        /// <param name="endPointA"></param>
        /// <param name="midPointB"></param>
        /// <param name="endPointC"></param>
        /// <returns>Used to determine clockwise/counterclockwise for angle drawer.</returns>
        public static double GetArea(double ax, double ay, double bx, double by, double cx, double cy)
        {
            return ((ax - cx) * (by - cy) - (ay - cy) * (bx - cx)) / 2;
        }
    }
}
